package com.haixiaoke.saas.enterprise.account.yima;

import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.saas.account.yima.config.YiMaConfiguration;
import com.haixiaoke.saas.account.yima.doman.YiMaDto;
import com.haixiaoke.saas.account.yima.service.YiMaService;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.enums.YiMaImageTypeEnum;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.utils.GsonUtil;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author qixi
 * @date 2024/6/22
 */
@RestController
@RequestMapping("/enterprise/account/yima")
public class YiMaController extends BaseController {
    @Autowired
    private YiMaService yiMaService;
    @Autowired
    private YiMaConfiguration yiMaConfiguration;

    /**
     * 上传图片
     *
     * @param type
     * @param file
     * @return
     */
    @PostMapping("/upload/image/{type}")
    public AjaxResult upload(@PathVariable("type") String type, MultipartFile file) {
        try {
            YiMaImageTypeEnum yiMaImageTypeEnum = YiMaImageTypeEnum.getEnumByCode(type);
            if (yiMaImageTypeEnum == null) {
                throw new ServiceException("图片类型错误");
            }
            String description = yiMaImageTypeEnum.getDescription();
            String fileName = description + "_" + DateUtil.date() + ".jpg";

            byte[] bytes = file.getBytes();
            String base64 = Base64Encoder.encode(bytes);
            YiMaDto yiMaDto = new YiMaDto();
            yiMaDto.setImageContent(base64);
            yiMaDto.setImageName(fileName);
            yiMaDto.setType(type);
            String result = yiMaService.uploadPictures(yiMaDto);
            if (yiMaConfiguration.checkResult(result)) {
                Map<String, Object> map = GsonUtil.toMap(result);
                String id = ObjectUtil.toString(map.get("id"));
                return AjaxResult.success("上传成功", id);
            } else {
                logger.error("上传图片失败，原因：{}", result);
                return AjaxResult.error("上传失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("上传失败");
        }
    }


    // /**
    //  * 添加分账
    //  */
    // @PostMapping("/merchantIndividual")
    // public AjaxResult merchantIndividual(@RequestBody YiMaDto yiMaDto) {
    //     return success(yiMaService.merchantIndividual(yiMaDto));
    // }
    //
    // /**
    //  * 4.47.分账商户业务入驻
    //  */
    // @PostMapping("/merchantOpen")
    // public AjaxResult merchantOpen(@RequestBody YiMaDto yiMaDto) {
    //     return success(yiMaService.merchantOpen(yiMaDto));
    // }


}
